package pathfinding.grid.astar;

import pathfinding.core.Location;
import pathfinding.core.astar.SortedLocationList;


public class GridSortedLocationList implements SortedLocationList{
	
	//private LinkedList<GridLocationAstar> locationList;
	private MinHeap minHeap;
	
	public GridSortedLocationList(){
		//locationList = new LinkedList<GridLocationAstar>();
		minHeap = new MinHeap();
	}
	
	@Override
	public boolean hasNext() {
		return minHeap.size() > 0;
		//return locationList.size() > 0;
	}
	
	@Override
	public void add(Location loc){
		GridLocationAstar location = (GridLocationAstar)loc;
		addInOrder(location);
	}

	@Override
	public GridLocationAstar getNext() {
		if(minHeap.size() > 0){
			return minHeap.remove();
		}
		//if(locationList.size() > 0){
		//	return locationList.remove(0);
		//}
		return null; //TODO throw end of list exception
	}
	
	private void addInOrder(GridLocationAstar location){
		minHeap.add(location);
		/*GridLocationAstar tempLocation;
		if(locationList.size() == 0){
			locationList.add(location);
			return;
		}
		/*
		for(int i=0; i<locationList.size(); i++){
			tempLocation = locationList.get(i);
			if(location.getTotalDistance() < tempLocation.getTotalDistance()){
				locationList.add(i, location);
				return;
			}
		}*/
		//locationList.add(location);
	}
	
	public String toString(){
		String result = minHeap.size() + "";
		//String result = locationList.size() + "";
		return result;
	}

}
